package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.adapter.CityAndprovinceListAdapter;
import com.itboye.textileexpes.beans.CityBeans;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.util.ListToStringUtil;
import com.itboye.textileexpres.util.SPContants;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textillexepres.db.DBManager;

/**
 * 国家
 * 
 * @author sy
 * 
 */
public class ActivityDiZhiType extends BaseActivity implements OnClickListener {
	private BaseAdapter adapter;
	private ListView country_lv_list;
	private Handler handler;
	private SQLiteDatabase database;
	private ArrayList<CityBeans> mCityNames;
	private ImageView back;
	EditText edit_inputcity;
	private DBManager dbManager;
	public String city;
	int type = 1;// 1、直辖市 2、城市
	boolean searchType = true;// true、根据 城市搜索店铺 false、根据地区搜索店铺
	RelativeLayout relypoutGps, locaridonRealyout;
	String countryId;
	String titlee;
	String strCh;

	TextView title;
	CityBeans cityModel;

	TextView LocationResult;
	String from;
	ImageView img_other;

	private ListToStringUtil stringUtil;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_city;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		country_lv_list = (ListView) findViewById(R.id.country_lv_list);
		cityModel = new CityBeans();
		dbManager = new DBManager(this);
		dbManager.openDateBase();
		database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/"
				+ DBManager.DB_NAME, null);
		mCityNames = getCityNames();
		handler = new Handler();
		setAdapter(mCityNames);
		title.setText("地区选择");
		img_other.setVisibility(View.GONE);
		SPUtils.put(ActivityDiZhiType.this, null, SPContants.COUNTRY, "");
		SPUtils.put(ActivityDiZhiType.this, null, SPContants.PROVINCE,"");
		SPUtils.put(ActivityDiZhiType.this, null, SPContants.COUNTRY, "");
		SPUtils.put(ActivityDiZhiType.this, null, SPContants.PROVINCECITY,"");
		SPUtils.put(ActivityDiZhiType.this, null, SPContants.AREA, "");
		SPUtils.put(ActivityDiZhiType.this, null, SPContants.COUTRYID,"");
		SPUtils.put(ActivityDiZhiType.this, null, SPContants.PROVINCECITYID, "");
		SPUtils.put(ActivityDiZhiType.this, null, SPContants.PROVINCEID,"");
		SPUtils.put(ActivityDiZhiType.this, null, SPContants.AREAID, "");
		
		country_lv_list.setOnItemClickListener(new CityListOnItemClick());
	}

	/**
	 * 从数据库获取城市数据
	 * 
	 * @return
	 */
	private ArrayList<CityBeans> getCityNames() {
		ArrayList<CityBeans> names = new ArrayList<CityBeans>();
		Cursor cursor = database.rawQuery(
				"SELECT * FROM common_country  ORDER BY country_name", null);

		while (cursor.moveToNext()) {

			CityBeans cityModel = new CityBeans();
			// 游标查询并赋值到CityBeans中
			cityModel.setCountry_name(cursor.getString(cursor
					.getColumnIndex("country_name")));
			cityModel.setId(cursor.getString(cursor.getColumnIndex("id")));

			cityModel.setCountryTelPrefix(cursor.getString(cursor
					.getColumnIndex("country_tel_prefix")));

			names.add(cityModel);
		}
		return names;
	}

	/**
	 * 城市列表点击事件
	 * 
	 * @author sy
	 * 
	 */
	class CityListOnItemClick implements OnItemClickListener {

		@SuppressWarnings("static-access")
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
				long arg3) {

			searchType = true;
			cityModel = (CityBeans) country_lv_list.getAdapter().getItem(pos);
			city = cityModel.getCountry_name();
			cityModel.getCountryCode();

			String CityId = cityModel.getId();
			getPrivateNames(CityId);
			finish();
			country_lv_list.setSelection(-1);// 设置选择项为第一项

		}
	}

	@SuppressWarnings("unused")
	private String getPrivateNames(String codeId) {
		ArrayList<CityBeans> names = new ArrayList<CityBeans>();
		Cursor cursor = database.rawQuery(
				"SELECT * FROM common_province where countryid=" + codeId
						+ " ORDER BY province", null);
		while (cursor.moveToNext()) {

			CityBeans cityModel = new CityBeans();
			cityModel.setCountry_name(cursor.getString(cursor
					.getColumnIndex("province")));

			cityModel.setId(cursor.getString(cursor
					.getColumnIndex("provinceID")));

			names.add(cityModel);
		}

		String country_tel = cityModel.getCountryTelPrefix();

		if (codeId.equals("1")) {
			Intent intent = new Intent(ActivityDiZhiType.this,
					ActivityAddreeStart.class);
			intent.putExtra("id", cityModel.getId());
			intent.putExtra("titlee", cityModel.getCountry_name());
			intent.putExtra("from", from);
			startActivity(intent);
		} else if (stringUtil.ListToString(names) == null
				|| stringUtil.ListToString(names).equals("")
				|| names.size() <= 0) {
			Intent intent2 = new Intent("AddressBeans");
			intent2.putExtra("from", from);
			sendBroadcast(intent2);
			SPUtils.put(ActivityDiZhiType.this, null, SPContants.AddressBeans,
					city);

			SPUtils.put(ActivityDiZhiType.this, null, SPContants.COUNTRY, city);
			SPUtils.put(ActivityDiZhiType.this, null, SPContants.COUTRYID,
					cityModel.getId());
			SPUtils.put(ActivityDiZhiType.this, null,
					SPContants.CITYCOUNTRY_TEL_PREFIXUID, country_tel);

		} else {
			Intent intent = new Intent(ActivityDiZhiType.this,
					AddressSecondActivity.class);
			intent.putExtra("countrtID", cityModel.getId());
			intent.putExtra("country", cityModel.getCountry_name());
			intent.putExtra("COUNTRY", city);
			intent.putExtra("from", from);
			startActivity(intent);

			System.out.println("nullnull" + names.size());
		}
		return stringUtil.ListToString(names);
	}

	/**
	 * 为ListView设置适配器
	 * 
	 * @param list
	 */
	private void setAdapter(List<CityBeans> list) {

		if (list != null) {
			adapter = new CityAndprovinceListAdapter(this, list);
			country_lv_list.setAdapter(adapter);
		}

	}

	/***
	 * 跳转到中国区域
	 */

	public void onIntent() {
		Intent intent = new Intent(ActivityDiZhiType.this,
				ChinaIsCityAreaActivity.class);
		intent.putExtra("id", cityModel.getId());
		intent.putExtra("titlee", cityModel.getCountry_name());
		intent.putExtra("from", from);
		startActivity(intent);
	}

	@SuppressWarnings("static-access")
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			this.finish();
			break;
		case R.id.relypoutGps:

			String CityId = cityModel.getId();
			System.out.println("cityID" + CityId);
			if (CityId == null) {
				Intent intent = new Intent(ActivityDiZhiType.this,
						ChinaIsCityAreaActivity.class);
				intent.putExtra("id", "1");
				intent.putExtra("titlee", "中国");
				intent.putExtra("from", from);
				startActivity(intent);
			} else if (CityId.equals("1")) {
				onIntent();
			} else if (!CityId.equals("1")) {
				Intent intent2 = new Intent("City");
				intent2.putExtra("from", from);
				sendBroadcast(intent2);
				SPUtils.put(ActivityDiZhiType.this, null,
						SPContants.COUNTRYNMAW, city);
				SPUtils.put(ActivityDiZhiType.this, null,
						SPContants.CITYCOUNTRY_TEL_PREFIXUID,
						cityModel.getCountry_name());

			} else {
				Intent intent = new Intent(ActivityDiZhiType.this,
						GetCityActivity.class);
				intent.putExtra("id", cityModel.getId());
				intent.putExtra("titlee", cityModel.getCountry_name());
				intent.putExtra("from", from);
				startActivity(intent);

			}
			finish();
			break;
		case R.id.locaridonRealyout:
			finish();
			break;
		default:
			break;
		}
	}

}